<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /*状态及原因
        1.状态 pendig , fullfilled, rejected
        2.原因
        3.调整resolve, reject方法
        3.状态不可逆
        */
        const PENDING = 'pending'
        const FULFILLED = 'fulfilled'
        const REJECTED = 'rejected'
        class HMpPromise {
            //状态与原因
            state = PENDING
            result = undefined
            constructor(executor) {
                //改状态， pending -> fulfilled
                //记录原因 
                const resolve = (result) => {
                    if (this.state === PENDING) {
                        this.state = FULFILLED
                        this.result = result
                    }
                }
                //改状态， pending -> rejected
                const reject = (result) => {
                    if (this.state === PENDING) {
                        this.state = REJECTED
                        this.result = result
                    }
                }
                executor(resolve, reject)
            }
        }



        const p = new HMpPromise((resolve, reject) => {
            console.log('执行器函数被调用')
            resolve('成功')
            reject('失败')
        })
    </script>
</body>

</html>